var tableIns;
var searchParam = null;//用于存表单查询的条件参数
layui.use(['form','layer','table','laytpl','element'],function(){
    var element = layui.element;
    var form = layui.form,
        layer=layui.layer;
    $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    tableIns = table.render({
        elem: '#industryTypesTable',
        url : baseUrl + '/industryType/queryByPage',
        cellMinWidth : 95,
        height : "full-175",
        page : true,
        request: {
            pageName: 'page' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,statusCode: 1 //成功的状态码，默认：0
            ,msgName: 'msg' //状态信息的字段名称，默认：msg
            ,countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'records' //数据列表的字段名称，默认：data
        },loading: true
        ,page: {
            id:"id",
            limit: 10
            ,limits: [10, 20, 30, 40, 50]
            ,curr: 1
            ,groups: 5
            ,theme: '#5FB878'
            ,first: '首页'
            ,last: '尾页'
            ,prev: '<em><i class="layui-icon">&#xe603;</i></em>'
            ,next: '<em><i class="layui-icon">&#xe602;</i></em>'
            ,layout: [ 'prev', 'page', 'next', 'skip','count']
        },
        cols : [[
            {field: 'name', title: '类型名称', width:'15%',minWidth:80},
            {field: 'parentName', title: '所属类型名称', width:'15%',minWidth:80},
            {field: 'createUserName', title: '创建人', align:'center', width:'15%',minWidth:80},
            {field: 'modTime', title: '修改时间', align:'center', width:'15%',minWidth:100},
            {title: '操作', templet:'#bar',fixed:"right",align:"center",width:'40%',minWidth:150}
        ]],done: function(res, curr, count){
            intTableColorChange();
        }
        ,text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
    });


    //列表操作
    table.on('tool(industryTypesTable)', function(obj){
        var layEvent = obj.event,
            param = obj.data;
        if(layEvent === 'edit'){ //编辑
            edit(param.id);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('您确定删除此类型？',{icon:3, title:'提示信息'},function(index){
                var load = window.parent.layer.msg('执行中，请稍候',{icon: 16,time:false,shade:0.8});
                $.ajax({
                    type: "POST",
                    url:  baseUrl+'/industryType/del?id='+param.id,
                    dataType: 'json',
                    success: function (data) {
                        window.parent.layer.close(load);
                        if(data.code<=0){
                            layer.msg(data.msg);
                            layer.close(index);
                        }else{
                            reloadTable();
                            layer.close(index);
                        }
                    },
                    error: function () {
                        window.parent.layer.close(load);
                        layer.msg("保存失败");
                        layer.close(index);
                    }
                });
            });
        }
    });


    // 提交查询条件表单
    form.on('submit(query)', function(data){
        searchParam = data.field;
        reloadTable();
    });

    //编辑默认分支
    function edit(id){
        var params = "?id="+id;
        currentIndex = layer.open({
            title : "新增类型",
            type : 2,
            content : [baseUrl+"/industryType/saveOrUpdateIndustryType"+params],
            resize:true,
            maxmin:false,
            isOutAnim:true,
            scrollbar:true,
            area : ['570px','330px'],
            btn: ['保存', '取消'],
            yes: function (index, layero) {
                var body = layer.getChildFrame('body', index); //得到子页面层的BODY
                var button = body.find('[lay-submit]'); //获取子窗口表单提交按钮dom
                button.click();
                reloadTable();
            }
            , btn2: function (index, layero) {
                layer.close(index);
            }
        });
    }
    $("#add_btn").click(function(){
        edit('');
    });

    //选中行时，高亮显示
    function intTableColorChange(){
        $(".layui-table-body tr").click(function(e){
            $(".layui-table-body  tr ").css("background","");
            $(this).css("background","#e6e6e6");
        });
    }

});


//重新加载table的数据
function reloadTable() {
    var load = layer.load();
    var whereParam = {};
    if(searchParam!=null){
        whereParam = searchParam;
    }
    tableIns.reload({
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: whereParam
    });
    layer.close(load);
}